package com.wxxymaker.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.hook.annotation.AdminRequire;
import com.wxxymaker.model.entity.Member;
import com.wxxymaker.service.MemberService;
import io.github.biezhi.anima.page.Page;

/**
 * @Author: Soft
 * @Date: 2018/7/13 10:37
 * @Desc:
 */
@AdminRequire
@Path(value = "/member", restful = true)
public class MemberController {

    @Inject
    private MemberService service;

    @PostRoute("/add")
    public RestResponse add(Member member) {
        Member add = service.add(member);
        if (add != null && add.getId() > 0) {
            return RestResponse.ok(add);
        }
        return RestResponse.fail();
    }

    @PostRoute("/delete/:id")
    public RestResponse delete_(@PathParam int id) {
        Member delete = service.delete(id);
        if (delete != null) {
            return RestResponse.ok(delete);
        }
        return RestResponse.fail();
    }

    @PostRoute("/change")
    public RestResponse change(Member member) {
        if (member != null && member.getId() > 0) {
            int change = service.change(member);
            if (change > 0) {
                return RestResponse.ok();
            }
        }
        return RestResponse.fail();
    }

    @PostRoute("/page/:no")
    public RestResponse page(@PathParam int no) {
        Page<Member> page = service.page(no);
        if (page != null && page.getTotalRows() > 0) {
            return RestResponse.ok(page);
        }
        return RestResponse.fail();
    }

    @PostRoute("/like")
    public RestResponse like(@Param String name) {
        Page<Member> like = service.like(name);
        if (like != null && like.getTotalRows() > 0) {
            return RestResponse.ok(like);
        }
        return RestResponse.fail();
    }
}
